//
//  NSHandle.m
//  MarryMe
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "NSHandle.h"
#import "HostModel.h"
#import "CaseModel.h"
#import "AllCaseModel.h"
#import "UIImageView+WebCache.h"
static NSHandle *handle = nil;
@implementation NSHandle


+(instancetype)shareHandle{
    return [[self alloc]init];
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (handle == nil) {
            handle = [super allocWithZone:zone];
        }
    });
    return handle;
}


-(void)getNetWorkWithStrUrl:(NSString *)strUrl
             WithHTTPMethod:(NSString *)method
               WithHTTPBody:(NSString *)body
                  WithBlock:(UpdateBlock)block{
    
    //创建url
    NSURL *url = [[NSURL alloc] initWithString:strUrl];
    //创建一个请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    //判断请求方式设置body
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:method];
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
    }
    //设置一个会话
    NSURLSession *session = [NSURLSession sharedSession];
    //设置一个任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error == nil) {
            NSMutableDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = resultDic[@"data"];
            NSArray *merchantsArray = dataDic[@"merchants"];
            self.HostArray = [NSArray array];
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *hostDic in merchantsArray) {
                HostModel *model = [[HostModel alloc]init];
                [model setValuesForKeysWithDictionary:hostDic];
                model.ids = hostDic[@"id"];
                model.contact_phone = [hostDic[@"contact_phone"] firstObject];
          
                [tempArray addObject:model];
            }
            self.HostArray = [NSArray arrayWithArray:tempArray];
           // NSLog(@"HostArray =%@",self.HostArray);
            dispatch_async(dispatch_get_main_queue(), ^{
                block(self.HostArray);
            });
        }
    }];
    [dataTask resume];
    
}

-(void)getDataWithStrUrl:(NSString *)strUrl
          WithHTTPMethod:(NSString *)method
            WithHTTPBody:(NSString *)body
               WithBlock:(UpdateBlock)block{
    
    //创建url
    NSURL *url = [[NSURL alloc] initWithString:strUrl];
    //创建一个请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    //判断请求方式设置body
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:method];
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
    }
    //设置一个会话
    NSURLSession *session = [NSURLSession sharedSession];
    //设置一个任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error == nil) {
            NSMutableDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *workDic = resultDic[@"work"];
            
            NSArray *mediaArray = workDic[@"media_items"];
            NSMutableArray *tempArray = [NSMutableArray array];
            CaseModel *model = [[CaseModel alloc]init];
            NSDictionary *merchantDic = workDic[@"merchant"];
            model.ids = merchantDic[@"id"];
            model.name = merchantDic[@"name"];
            model.logo_path = merchantDic[@"logo_path"];
            model.title = workDic[@"title"];
            model.describe = workDic[@"describe"];
            [tempArray addObject:model];
            for (NSDictionary *dataDic in mediaArray) {
                CaseModel *model = [[CaseModel alloc]init];
//                model.title = workDic[@"title"];
//                model.describe = workDic[@"describe"];
                [model setValuesForKeysWithDictionary:dataDic];
//                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.media_path]];
//                UIImage *image = [UIImage imageWithData:data];
//                model.width = [NSString stringWithFormat:@"%f",image.size.width];
//                model.height = [NSString stringWithFormat:@"%f",image.size.height];
//                UIImageView *img = [[UIImageView alloc]init];
//                [img sd_setImageWithURL:[NSURL URLWithString:model.media_path]];
//                model.width = [NSString stringWithFormat:@"%f",img.image.size.width];
//                model.height = [NSString stringWithFormat:@"%f",img.image.size.height];
                
                [tempArray addObject:model];
            }
           
            self.dataArray = [NSArray arrayWithArray:tempArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                block(self.dataArray);
            });
        }
    }];
    [dataTask resume];
}

-(void)getAllCasesWithStrUrl:(NSString *)strUrl
              WithHTTPMethod:(NSString *)method
                WithHTTPBody:(NSString *)body
                   WithBlock:(UpdateBlock)block{
    //创建url
    NSURL *url = [[NSURL alloc] initWithString:strUrl];
    //创建一个请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    //判断请求方式设置body
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:method];
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
    }
    //设置一个会话
    NSURLSession *session = [NSURLSession sharedSession];
    //设置一个任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error == nil) {
            NSMutableDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *workArray = resultDic[@"works"];
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *workDic in workArray) {
              AllCaseModel  *model = [[AllCaseModel alloc]init];
                [model setValuesForKeysWithDictionary:workDic];
                model.ids = workDic[@"id"];
                [tempArray addObject:model];
            }
            self.caseArray = [NSArray arrayWithArray:tempArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                block(self.caseArray);
            });
        }
    }];
    [dataTask resume];
}


-(void)getLabelWithStrUrl:(NSString *)strUrl
           WithHTTPMethod:(NSString *)method
             WithHTTPBody:(NSString *)body
                WithBlock:(UpdateBlock)block{
    
    //创建url
    NSURL *url = [[NSURL alloc] initWithString:strUrl];
    //创建一个请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    //判断请求方式设置body
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:method];
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
    }
    //设置一个会话
    NSURLSession *session = [NSURLSession sharedSession];
    //设置一个任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error == nil) {
            NSMutableDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *workArray = resultDic[@"works"];
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *workDic in workArray) {
                AllCaseModel *model = [[AllCaseModel alloc]init];
                [model setValuesForKeysWithDictionary:workDic];
                model.ids = workDic[@"id"];
                NSDictionary *merchantDic = workDic[@"merchant"];
                model.name = merchantDic[@"name"];
                [tempArray addObject:model];
            }
            self.labelArray = [NSArray arrayWithArray:tempArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                block(self.labelArray);
            });
        }
    }];
    [dataTask resume];
    
    
}







@end
